<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" @filterChange="handleSearch" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="allocationInfo"
      selectionType="single"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      :onSyncTableData="handleSyncTableData"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="exportExcel" :disabled="isExport">导出</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="showPanelIn" title="调拨单明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <showItemsIn @close="showPanelIn = false" :data="itemsData" />
    </BaseDialog>
    <BaseDialog :visible.sync="showPanelOut" title="调拨单明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <showItemsOut @close="showPanelOut = false" :data="itemsData1" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { confirmAction, notifyAction, exportExcelFile } from '@/utils';
import { queryAllocationOrder, queryAllocationIn, queryAllocationOut, exportData } from '@/api/part/allocation';
import showItemsIn from './showItemsIn';
import showItemsOut from './showItemsOut';

export default {
  mixins: [dictionary],
  components: {
    showItemsIn,
    showItemsOut
  },
  data() {
    return {
      topFilterList: this.createTopFIlterList(),
      showPanelOut: false,
      showPanelIn: false,
      columns: this.createTableColumns(),
      fetchapi: queryAllocationOrder,
      fetchParams: { xhrAbort: true },
      itemsData: {},
      itemsData1: {},
      showPanel: false,
      isExport: true,
      selectedRows: [],
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    this.topFilterList.find(x => x.fieldName == 'orderStatus').itemList = this.createDictList('9031');
  },
  methods: {
    createTopFIlterList() {
      return [
        {
          type: 'INPUT',
          label: '调拨单号',
          fieldName: 'allocateNo'
        },
        {
          type: 'SELECT',
          label: '调拨状态',
          fieldName: 'orderStatus',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '调拨类型',
          fieldName: 'orderType',
          itemList: [
            { text: '调拨入库', value: 'in' },
            { text: '调拨出库', value: 'out' }
          ]
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'orderDateBegin|orderDateEnd',
          dateType: 'daterange'
        },
        {
          type: 'RANGE_DATE',
          label: '入库日期',
          fieldName: 'finishedInDateBegin|finishedInDateEnd',
          dateType: 'daterange'
        },
        {
          type: 'RANGE_DATE',
          label: '出库日期',
          fieldName: 'finishedOutDateBegin|finishedOutDateEnd',
          dateType: 'daterange'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.showItems(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '调拨单号',
          dataIndex: 'allNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '调入经销商名称',
          dataIndex: 'inName',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '调出经销商名称',
          dataIndex: 'outName',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '调拨单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          dictItems: this.createDictList('9031'),
          filterItems: this.createDictList('9031'),
          showOverflowTooltip: true
        },
        {
          title: '调拨总数量',
          dataIndex: 'sumQuantity',
          sorter: true,
          summation: true,
          filter: true,
          filterType: 'number',
          precision: 2,
          showOverflowTooltip: true
        },
        {
          title: '调拨总金额',
          dataIndex: 'sumAmount',
          sorter: true,
          summation: true,
          filter: true,
          filterType: 'number',
          precision: 2,
          showOverflowTooltip: true
        },
        {
          title: '申请日期',
          dataIndex: 'orderDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true
        },
        {
          title: '入库日期',
          dataIndex: 'inDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true
        },
        {
          title: '出库日期',
          dataIndex: 'outDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true
        },
        {
          title: '作废日期',
          dataIndex: 'updatedAt',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true
        }
      ];
    },
    async showItems(row) {
      if (row.allNo.substring(0, 2) == 'DR') {
        let res = await queryAllocationIn({ allocateInNo: row.allNo });
        // jsonToHump(res.data.rows[0]);
        console.log('res::', res);
        this.itemsData = res.data.rows[0];
        this.showPanelIn = true;
      } else {
        let res = await queryAllocationOut({ allocateOutNo: row.allNo });
        // jsonToHump(res.data.rows[0]);
        console.log('res::', res);
        this.itemsData1 = res.data.rows[0];
        this.showPanelOut = true;
      }
    },
    collapseHandle() {
      this.nextTick(() => {
        this.refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    selectChange(val) {
      this.selectedRows = val;
      if (this.selectedRows.length >= 1 && this.selectedRows[0].orderStatus == '90311006') {
        this.isExport = false;
      } else {
        this.isExport = true;
      }
    },
    // 点击导出
    async exportExcel() {
      if (this.selectedRows && this.selectedRows.length >= 1) {
        let queryData = this.selectedRows[0];
        let data = {
          // ...queryData,
          orderNo: queryData.allNo
        };
        let res = await exportData(data);
        exportExcelFile(res.data, queryData.allNo + '调拨明细');
      }
    },
    // 点击搜索
    handleSearch(val) {
      this.fetchParams = Object.assign({}, val);
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    }
  }
};
</script>

<style scoped></style>
